/***** init section, begin *****/
$(function() {
    initPortlets();
    updatePortletsContent();
});

function initPortlets()
{
    $(".wacPortletColumn").sortable({
        connectWith: '.wacPortletColumn'
    });

    $(".wacPortlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
    .find(".wacPortlet-header")
    .addClass("ui-widget-header ui-corner-all")
    .prepend('<span class="ui-icon ui-icon-minusthick"></span>')
    .end()
    .find(".wacPortlet-content");

    $(".wacPortlet-header .ui-icon").click(function() {
        $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
        $(this).parents(".wacPortlet:first").find(".wacPortlet-content").toggle();
    });

    $(".wacPortletColumn").disableSelection();
}

function updatePortletsContent()
{
    //    $("#portlet_customer_order > .wacPortlet-content").css("border","3px solid red");
    var params = {
        "data_format" : 'json'
    };

    wacShowPortletLoading("#portlet_customer_order", "");
    wacShowPortletLoading("#portlet_production_order", "");
    wacShowPortletLoading("#portlet_weave_order", "");
    wacShowPortletLoading("#portlet_final_cloth_form", "");

    $.ajax({
        url: BASE_URL + "default/getPortletsInfo",
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            updatePortletsContentCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function updatePortletsContentCallBack(jsonData)
{
    var tmpObj = null;
    var id="", code="", deliveryDate="0", rawId="", stat;
    if(jsonData.info.status == wacOperationStatus.Error)
    {
        wacShowTips(jsonData.info.error_info);
    }
    else
    {
        // for cusotmer order, begin
        tmpObj = $("#portlet_customer_order > .wacPortlet-content ul");
        if(tmpObj.length>0){
            tmpObj.empty();
            if(jsonData.items.customerOrders!=false)
            {
                for(i=0;i<$(jsonData.items.customerOrders).length;i++)
                {
                    id = jsonData.items.customerOrders[i].id;
                    code = jsonData.items.customerOrders[i].code;
                    stat = jsonData.items.customerOrders[i].status;
                    rawId = (stat!=wacFormStatus.audited) ? "1002":"1003";
                    deliveryDate = jsonData.items.customerOrders[i].delivery_date;
//                    wacDebugLog("<li onclick='javascript:triggerOnPartlet(\""+rawId+"\",\""+code+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'");
                    $("<li onclick='javascript:triggerOnPartlet(\"customerOrder\",\""+rawId+"\",\""+code+"\",\""+stat+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'"+code+"'</strong> 将于" + wacFormatDate(deliveryDate) + "到期发货</li>").appendTo(tmpObj);
                }
            }
        }
        wacHideBlockUI("#portlet_customer_order");
        // for cusotmer order, end

        // for production order, begin
        tmpObj = $("#portlet_production_order > .wacPortlet-content ul");
        if(tmpObj.length>0){
            tmpObj.empty();
            if(jsonData.items.productionOrders!=false)
            {
                for(i=0;i<$(jsonData.items.productionOrders).length;i++)
                {
                    id = jsonData.items.productionOrders[i].id;
                    code = jsonData.items.productionOrders[i].code;
                    stat = jsonData.items.productionOrders[i].status;
                    rawId = (stat!=wacFormStatus.audited) ? "1004":"1005";
                    deliveryDate = jsonData.items.productionOrders[i].delivery_date;
//                    wacDebugLog("<li onclick='javascript:triggerOnPartlet(\""+rawId+"\",\""+code+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'");
                    $("<li onclick='javascript:triggerOnPartlet(\"productionOrder\",\""+rawId+"\",\""+code+"\",\""+stat+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'"+code+"'</strong> 将于" + wacFormatDate(deliveryDate) + "到期发货</li>").appendTo(tmpObj);
                }
            }
        }
        wacHideBlockUI("#portlet_production_order");        
        // for production order, end

        // for weave order, begin
        tmpObj = $("#portlet_weave_order > .wacPortlet-content ul");
        if(tmpObj.length>0){
            tmpObj.empty();
            if(jsonData.items.weaveOrders!=false)
            {
                for(i=0;i<$(jsonData.items.weaveOrders).length;i++)
                {
                    id = jsonData.items.weaveOrders[i].id;
                    code = jsonData.items.weaveOrders[i].code;
                    stat = jsonData.items.weaveOrders[i].status;
                    rawId = (stat!=wacFormStatus.audited) ? "1032":"1033";
                    deliveryDate = jsonData.items.weaveOrders[i].delivery_date;
//                    wacDebugLog("<li onclick='javascript:triggerOnPartlet(\""+rawId+"\",\""+code+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'");
                    $("<li onclick='javascript:triggerOnPartlet(\"weaveOrder\",\""+rawId+"\",\""+code+"\",\""+stat+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'"+code+"'</strong> 将于" + wacFormatDate(deliveryDate) + "到期发货</li>").appendTo(tmpObj);
                }
            }
        }
        wacHideBlockUI("#portlet_weave_order");
        // for weave order, end

        // for finalCloth form, begin
        tmpObj = $("#portlet_final_cloth_form > .wacPortlet-content ul");
        if(tmpObj.length>0){
            tmpObj.empty();
            if(jsonData.items.finalClothForms!=false)
            {
                for(i=0;i<$(jsonData.items.finalClothForms).length;i++)
                {
                    id = jsonData.items.finalClothForms[i].id;
                    code = jsonData.items.finalClothForms[i].code;
                    stat = jsonData.items.finalClothForms[i].status;
                    rawId = (stat!=wacFormStatus.audited) ? "1072":"1073";
                    deliveryDate = jsonData.items.finalClothForms[i].delivery_date;
//                    wacDebugLog("<li onclick='javascript:triggerOnPartlet(\""+rawId+"\",\""+code+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'");
                    $("<li onclick='javascript:triggerOnPartlet(\"finalCloth\",\""+rawId+"\",\""+code+"\",\""+stat+"\");' class='wacNotice wacCursor' style='margin-left: -25px;'><strong>'"+code+"'</strong> 将于" + wacFormatDate(deliveryDate) + "到期发货</li>").appendTo(tmpObj);
                }
            }
        }
        wacHideBlockUI("#portlet_final_cloth_form");
        // for weave order, end

    }    
    
    //   wacDebugLog($.dump(jsonData));
}

function triggerOnPartlet(mn, id, code, stat)
{
//    $("#appStorehouseMenuTabs").trigger({type:"wacOrderSearch",rawId:"1002",orderCode:"k4"});
    $("#appStorehouseMenuTabs").trigger({type:"wacOrderSearch", moduleName:mn ,rawId:id, orderCode:code, status:stat});

//    $("#appStorehouseMenu").setSelection("1002");
}